package com.poorstu.manager.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.poorstu.dao.BursaryDao;
import com.poorstu.dao.FlowDao;
import com.poorstu.dao.LoadDao;
import com.poorstu.dao.LoginDao;
import com.poorstu.dao.PoorStudentDao;
import com.poorstu.manager.BursaryManager;
import com.poorstu.model.AuditsMod;
import com.poorstu.model.BursaryMod;
import com.poorstu.model.ClassMod;
import com.poorstu.model.FlowLineMod;
import com.poorstu.model.FlowMod;
import com.poorstu.model.LoginMod;
import com.poorstu.model.StuPoorMod;

public class BursaryManagerImpl implements BursaryManager {
     private BursaryDao bursaryDao;
     private	FlowDao flowDao;
     private	LoginDao loginDao;
     private PoorStudentDao  poorStudentDao;
     private	LoadDao loadDao;
      
	public void deleteBursary(int bursaryId) {
		BursaryMod bursary=	bursaryDao.selectBursaryByBursaryId(bursaryId);
		bursaryDao.deleteBursary(bursary);
		
	}

	public List<BursaryMod> selectAll() {
		return null;
	}

	public void updateBursary(int bursaryId,String bursaryApplyer) {
		BursaryMod bursary=	bursaryDao.selectBursaryByBursaryId(bursaryId);
		bursary.setBurTime(new Date());
		bursary.setBurap(bursaryApplyer);
		bursaryDao.updateBursary(bursary);
	}

	
	public void setBursaryDao(BursaryDao bursaryDao) {
		this.bursaryDao = bursaryDao;
	}

	public List<BursaryMod> getBursary(int allUserId) {
		
		try{
		List<AuditsMod> auditList=bursaryDao.selectAuditById(allUserId);
		List<BursaryMod> bursaryList=new ArrayList<BursaryMod>();
		for(int i=0;i<auditList.size();i++){
			int auditId=auditList.get(i).getAuditId();
			 BursaryMod bursary=bursaryDao.selectBursaryByauditId(auditId);
//			 AuditsMod auditMod=new  AuditsMod();
//			 auditMod.setAudStatus(bursary.getAudits().getAudStatus());
//			 bursary.setAudits(auditMod);
			 bursaryList.add(bursary);
		}
		return bursaryList;
	}
	catch(Exception e){
		e.printStackTrace();
		return null;
	}
}

	public int addBursary(int allUserId, String bursary) {
    AuditsMod audit=poorStudentDao.selectStuPoor(allUserId).get(0);
	if(audit.getAudStatus()!=100)
	{
		return 99;
	}else{
		int flowId=3;
		FlowMod flow=flowDao.selcetFlowById(flowId);
		LoginMod login=loginDao.selectUserById(allUserId);
		AuditsMod auditMod=new  AuditsMod();
		auditMod.setAudStatus(0);
		auditMod.setFlow(flow);
		auditMod.setLogins(login);
		auditMod.setTimes(new Date());
		loadDao.saveAudit(auditMod);
		
		BursaryMod bursaryMod=new BursaryMod();
		bursaryMod.setBurTime(new Date());
		bursaryMod.setBurap(bursary);
		bursaryMod.setAudits(auditMod);
		bursaryDao.saveBurary(bursaryMod);
		
		return 1;
	}
	
		
	
	
	
	
	}

	public void setFlowDao(FlowDao flowDao) {
		this.flowDao = flowDao;
	}

	public void setLoginDao(LoginDao loginDao) {
		this.loginDao = loginDao;
	}

	public void setPoorStudentDao(PoorStudentDao poorStudentDao) {
		this.poorStudentDao = poorStudentDao;
	}

	public void setLoadDao(LoadDao loadDao) {
		this.loadDao = loadDao;
	}

	
	
	public BursaryMod selectBursaryById(int bursaryId) {
		return	bursaryDao.selectBursaryByBursaryId(bursaryId);
	}

	public List<BursaryMod> AuditBursary(int roleId, int allUserId) {

		List<BursaryMod> bursaryModList=bursaryDao.selectAll();
		
		LoginMod login=loginDao.selectUserById(allUserId);
		int userClassId=login.getClassId().getClassId();
		int userCollegeId=login.getCollege().getCollegeId();
		
		List<BursaryMod> bursaryList=new ArrayList<BursaryMod>();
		for(int i=0;i<bursaryModList.size();i++){
			int status=	bursaryModList.get(i).getAudits().getAudStatus();
			int classId=bursaryModList.get(i).getAudits().getLogins().getClassId().getClassId();
			//当用户不属于莫一个班级是 但一定属于某个学院
			int usercollege=bursaryModList.get(i).getAudits().getLogins().getCollege().getCollegeId();
			
			//int cllegeId=loadList.get(i).getAuditss().getLogins().getClassId().getCollegeId().getCollegeId();
			List<FlowLineMod> flowLineList=bursaryModList.get(i).getAudits().getFlow().getFlowLineList();
			for(int j=0;j<flowLineList.size();j++)
			{
				if(userClassId==0&&status==flowLineList.get(j).getOrders()&&flowLineList.get(j).getRole().getRoleId()==roleId&&userCollegeId==usercollege){
					BursaryMod bursary=new  BursaryMod();
					LoginMod loginMod=new  LoginMod();
					ClassMod classes=new ClassMod();
					AuditsMod auditMod=new AuditsMod();
					
					loginMod.setName(bursaryModList.get(i).getAudits().getLogins().getStu().getStuName());
					loginMod.setUserAccount(bursaryModList.get(i).getAudits().getLogins().getUserAccount());
					classes.setClassName(bursaryModList.get(i).getAudits().getLogins().getClassId().getClassName());
					loginMod.setClassId(classes);
					
					auditMod.setLogins(loginMod);
					auditMod.setAudStatus(bursaryModList.get(i).getAudits().getAudStatus());
					
					bursary.setBurap(bursaryModList.get(i).getBurap());
					
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					bursary.setApptime(sdf.format(bursaryModList.get(i).getBurTime()));
					bursary.setBursaryId(bursaryModList.get(i).getBursaryId());
					bursary.setAudits(auditMod);
					
					bursaryList.add(bursary);
					
				} 
				
				else if(status==flowLineList.get(j).getOrders()&&flowLineList.get(j).getRole().getRoleId()==roleId&&classId==userClassId&&userCollegeId==usercollege)
				{
					BursaryMod bursary=new  BursaryMod();
					LoginMod loginMod=new  LoginMod();
					ClassMod classes=new ClassMod();
					AuditsMod auditMod=new AuditsMod();
					
					loginMod.setName(bursaryModList.get(i).getAudits().getLogins().getStu().getStuName());
					loginMod.setUserAccount(bursaryModList.get(i).getAudits().getLogins().getUserAccount());
					classes.setClassName(bursaryModList.get(i).getAudits().getLogins().getClassId().getClassName());
					loginMod.setClassId(classes);
					
					auditMod.setLogins(loginMod);
					auditMod.setAudStatus(bursaryModList.get(i).getAudits().getAudStatus());
					
					bursary.setBurap(bursaryModList.get(i).getBurap());
					
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					bursary.setApptime(sdf.format(bursaryModList.get(i).getBurTime()));
					bursary.setBursaryId(bursaryModList.get(i).getBursaryId());
					bursary.setAudits(auditMod);
					
					bursaryList.add(bursary);
				}
			}
		}
		
		return bursaryList;
	}

	public void AuditBursaryDetil(int audtiStatu, String remark, int bursaryId) {
		
		 int auditId=bursaryDao.selectBursaryByBursaryId(bursaryId).getAudits().getAuditId();	
		  AuditsMod auditMod=loadDao.selectAuditById(auditId);
		  
		  int flowId=auditMod.getFlow().getFlowId();
		  int orders=flowDao.selectOder(flowId);
		  int status=auditMod.getAudStatus();
		 
		  if(audtiStatu==0)
		  {
			  audtiStatu=99;
		  }
		  else  if(status==orders){
			  audtiStatu =100;
		  }
		  auditMod.setAudStatus(audtiStatu);
		  auditMod.setRemark(remark);
		  auditMod.setTimes(new Date());
		  loadDao.updateAudit(auditMod);
	}

	
	
	
	
	
	
}
